﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using SilverlightControlStyle.Resources;

namespace SilverlightControlStyle.Controls
{
    /// <summary>
    /// 右下角提示信息组
    /// </summary>
    public class WaitTipEx
    {
        private static Popup _topPopup;
        private static ListBox _listBox;
        private static ObservableCollection<TipInfo> _tipInfos = new ObservableCollection<TipInfo>();

        static WaitTipEx()
        {
            if (_topPopup == null)
            {
                _topPopup = new Popup();
                _listBox = new ListBox
                {
                    ItemsSource = _tipInfos,
                    Style = ResourceDictionaryManager.GetResource<Style>("XTipBoxStyle")
                };

                _listBox.SizeChanged += OnlistBox_SizeChanged;
                _topPopup.Child = _listBox;
            }

            Application.Current.Host.Content.Resized += OnContent_Resized;
        }

        /// <summary>
        /// 延时操作提示信息
        /// </summary>
        /// <param name="pKey">信息标识</param>
        /// <param name="pMessage">信息内容</param>
        public static void WaitMessage(string pKey, string pMessage)
        {
            if (string.IsNullOrWhiteSpace(pMessage))
            {
                for (int i = 0; i < _tipInfos.Count; i++)
                {
                    if (String.Compare(pKey, _tipInfos[i].Key, StringComparison.Ordinal) == 0)
                    {
                        _tipInfos.RemoveAt(i);
                        break;
                    }
                }
                if (_tipInfos.Count == 0)
                    _topPopup.IsOpen = false;
            }
            else
            {
                var tipInfo = new TipInfo
                {
                    Key = pKey,
                    Message = pMessage.Trim('.')
                };
                if (!_tipInfos.Contains(tipInfo))
                    _tipInfos.Add(tipInfo);

                if (!_topPopup.IsOpen)
                    _topPopup.IsOpen = true;
            }
        }

        /// <summary>
        /// ListBox控件改变大小时触发
        /// </summary>
        private static void OnlistBox_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox != null)
            {
                ChangePopupPosition(listBox);
            }
        }

        /// <summary>
        /// 主界面改变大小时触发
        /// </summary>
        private static void OnContent_Resized(object sender, EventArgs e)
        {
            ChangePopupPosition(_listBox);
        }

        /// <summary>
        /// 改变Popup提示信息的位置
        /// </summary>
        private static void ChangePopupPosition(ListBox pListBox)
        {
            _topPopup.HorizontalOffset = Application.Current.Host.Content.ActualWidth - pListBox.ActualWidth;
            _topPopup.VerticalOffset = Application.Current.Host.Content.ActualHeight - pListBox.ActualHeight - 5.0;
        }
    }

    /// <summary>
    /// 提示信息类
    /// </summary>
    public class TipInfo : INotifyPropertyChanged
    {
        private string _key;
        private string _message;

        /// <summary>
        /// 键值
        /// </summary>
        public string Key
        {
            get { return _key; }
            set
            {
                _key = value;
                OnPropertyChanged("Key");
            }
        }

        /// <summary>
        /// 提示信息
        /// </summary>
        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (obj.GetType() != GetType())
            {
                return false;
            }

            TipInfo temp = (TipInfo)obj;
            return Key.Equals(temp.Key);
        }

        public override int GetHashCode()
        {
            return Key.GetHashCode();
        }

        #region 变更通知
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string pPropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName));
            }
        }
        #endregion
    }
}
